package com.lantosec.hmrp.modules.eval.service.impl;

import com.lantosec.hmrp.modules.eval.entity.EvalDetailedItem;
import com.lantosec.hmrp.modules.eval.entity.EvalItem;
import com.lantosec.hmrp.modules.eval.entity.EvalTaskEvent;
import com.lantosec.hmrp.modules.eval.mapper.EvalDetailedItemMapper;
import com.lantosec.hmrp.modules.eval.service.IEvalDetailedItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.modules.eval.service.IEvalItemService;
import com.lantosec.hmrp.modules.eval.service.IEvalTaskItemService;
import com.lantosec.hmrp.modules.eval.vo.EvalDetailedItemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.context.annotation.Primary;
import lombok.extern.log4j.Log4j2;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 考评明细的项目 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-03-30
 */
@Service
@Primary
@Log4j2
public class EvalDetailedItemServiceImpl extends ServiceImpl<EvalDetailedItemMapper, EvalDetailedItem> implements IEvalDetailedItemService {
    @Autowired
    private IEvalItemService iEvalItemService;

    @Override
    public void saveEvalDetailedItem(List<EvalDetailedItemVO> evalDetailedItemVOList) {
        //追加一个考评项目则加一条数据
        evalDetailedItemVOList.forEach(parm->{
            EvalDetailedItem evalDetailedItem = parm.convertToEntity();
            String evalItemId = evalDetailedItem.getEvalItemId();
            EvalItem evalItem = iEvalItemService.getById(evalItemId);
            evalDetailedItem.setRewardOrientation(evalItem.getRewardOrientation());
            evalDetailedItem.setRewardUpLimit(evalItem.getRewardUpLimit());
            evalDetailedItem.setRewardLowLimit(evalItem.getRewardLowLimit());
            evalDetailedItem.setRewardStandard(evalItem.getRewardStandard());
            evalDetailedItem.setRewardType(evalItem.getRewardType());
            evalDetailedItem.setExtraItem(1);
            this.save(evalDetailedItem);
        });
    }

    @Override
    public void updateDetailedItem(Integer evalDetailedItemId) {
        EvalDetailedItem evalDetailedItem = this.getById(evalDetailedItemId);
        //得到考评奖惩方向的值
        Integer rewardOrientation = evalDetailedItem.getRewardOrientation();
        //得到考评奖惩标准值
        BigDecimal rewardStandard = evalDetailedItem.getRewardStandard();
        //如果考评奖惩方向是分则更新itemScore
        if(rewardOrientation == 1){
            evalDetailedItem.setItemScore(rewardStandard);
            this.updateById(evalDetailedItem);
        }else{
            //如果奖惩方向是钱则更新temMoney
            evalDetailedItem.setItemMoney(rewardStandard);
            this.updateById(evalDetailedItem);
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteEvalDetailItem(Integer evalDetailedItemId,String evalItemId) {
        //删除追加项目
        baseMapper.deleteEvalDetailItem(evalDetailedItemId,evalItemId);
        //查询追加项目是否有事件
        List<EvalTaskEvent> evalTaskEventList = baseMapper.selectEvalTaskEvent(evalDetailedItemId);
        if(null != evalTaskEventList && evalTaskEventList.size() > 0){
            //删除追加项目的事件
            baseMapper.deleteEvalTaskEvent(evalDetailedItemId);
            //删除事件的追加责任
            evalTaskEventList.forEach(param->{
                baseMapper.deleteEvalEventExtraduty(param.getEvalTaskEventId());
            });
        }
    }
}
